//
//  SISSReleasePassengerDeatailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/24.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSReleasePassengerDeatailViewController.h"
#import "SISSPassengerCooperationDetailModel.h"
#import "SISSNetworkingManager+MineCooperation.h"
#import "SISSReceivedRequestViewController.h"
#import "SISSMorePopupView.h"
#import "SISSMyReleaseCooperationViewController.h"

@interface SISSReleasePassengerDeatailViewController ()<SISSMorePopupViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *nameAndPurposeLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndCommunityLabel;
@property (weak, nonatomic) IBOutlet UITextField *budgetTextField;
@property (weak, nonatomic) IBOutlet UITextField *houseTypeTextField;
@property (weak, nonatomic) IBOutlet UITextField *areaTextField;
@property (weak, nonatomic) IBOutlet UITextField *intentionFloorTextField;
@property (weak, nonatomic) IBOutlet UITextField *decorationTextField;
@property (weak, nonatomic) IBOutlet UITextField *directionTextField;
@property (weak, nonatomic) IBOutlet UITextField *purchasePurposeTextField;
@property (weak, nonatomic) IBOutlet UILabel *otherDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *releaseTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *ownerPercentLabel;
@property (weak, nonatomic) IBOutlet UILabel *partnerPercentLabel;
@property (weak, nonatomic) IBOutlet UILabel *cooperationDescriptionLabel;

/* 客源合作详情模型 */
@property (strong, nonatomic) SISSPassengerCooperationDetailModel *passengerCooperationModel;

@end

@implementation SISSReleasePassengerDeatailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加右侧分享删除按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)];
}

- (void)setPassengerCooperationModel:(SISSPassengerCooperationDetailModel *)passengerCooperationModel {
    [self.view layoutIfNeeded];
    _passengerCooperationModel = passengerCooperationModel;
    
    self.releaseTimeLabel.text = passengerCooperationModel.cooperation_insert_time;
    self.cooperationDescriptionLabel.text = passengerCooperationModel.cooperation_desc;
    
    self.nameAndPurposeLabel.text = [NSString stringWithFormat:@"%@求购",passengerCooperationModel.buyer_name];
    self.regionAndCommunityLabel.text = [NSString stringWithFormat:@"%@-%@ %@",passengerCooperationModel.region,passengerCooperationModel.regionDetail,passengerCooperationModel.neighbourhood];
    self.budgetTextField.text = [NSString stringWithFormat:@"%.0f-%.0f万",passengerCooperationModel.minPrice,passengerCooperationModel.maxPrice];
    self.houseTypeTextField.text = passengerCooperationModel.houseType;
    self.areaTextField.text = [NSString stringWithFormat:@"%.0f-%.0f㎡",passengerCooperationModel.minArea,passengerCooperationModel.maxArea];
    self.intentionFloorTextField.text = (passengerCooperationModel.minFloor != 0 && passengerCooperationModel.maxFloor != 0) ? [NSString stringWithFormat:@"%zd-%zd层",passengerCooperationModel.minFloor,passengerCooperationModel.maxFloor] : @"暂无数据";;
    self.decorationTextField.text = passengerCooperationModel.decoration.length ? passengerCooperationModel.decoration : @"暂无数据";
    self.directionTextField.text = passengerCooperationModel.direction.length ? passengerCooperationModel.direction : @"暂无数据";
    self.purchasePurposeTextField.text = passengerCooperationModel.purpose.length ? passengerCooperationModel.purpose : @"暂无数据";
    self.otherDescriptionLabel.text = passengerCooperationModel.otherdesc.length ? passengerCooperationModel.otherdesc : @"暂无数据";
    
    self.ownerPercentLabel.text = [NSString stringWithFormat:@"%zd%@",passengerCooperationModel.commission_rate,@"%"];
    self.partnerPercentLabel.text = [NSString stringWithFormat:@"%zd%@",100 -  passengerCooperationModel.commission_rate,@"%"];
}

/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:@[@"分享", @"删除"]];
    moreView.delegate = self;
    [moreView show];
}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (index == 0) {
        // 根据身份证判断显示 先生、女士
        NSString *fisrtName = [self.passengerCooperationModel.name substringToIndex:1];
        NSInteger idNum = [[self.passengerCooperationModel.idNumber substringWithRange:NSMakeRange(self.passengerCooperationModel.idNumber.length - 2, 1)] integerValue];
        
        NSString *genderStr = (idNum % 2) == 0 ? @"女士" : @"先生";
        NSString *title = [NSString stringWithFormat:@"%@%@ 求购",fisrtName,genderStr];
        NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@-%@ | %@",@(self.passengerCooperationModel.minPrice),@(self.passengerCooperationModel.maxPrice),self.passengerCooperationModel.region,self.passengerCooperationModel.regionDetail,self.passengerCooperationModel.houseType];
        
        NSString *webpageUrl = @"";
        if (self.passengerCooperationModel.demandType == 1) { // 线上
            webpageUrl = [NSString stringWithFormat:@"%@/?#!/buyerDetail/%@/",WEB_BASE_URL,self.passengerCooperationModel.ID];
        } else { // 线下
            webpageUrl = [NSString stringWithFormat:@"%@/?#!/offlineDemandDetail/%@",WEB_BASE_URL,self.passengerCooperationModel.ID];
        }
        
        [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:nil];
    } else {
        NSString *str = @"删除后不可恢复，您确定要删除吗？";
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.alignment = NSTextAlignmentLeft;
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName: COLOR(0x334A60), NSParagraphStyleAttributeName: paragraphStyle}];
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"删除" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [alertC setValue:attr forKey:@"attributedMessage"];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        [alertC addAction:cancelAction];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确定");
            
            [self deleteClickAction];
        }];
        [alertC addAction:okAction];
        
        [self presentViewController:alertC animated:YES completion:nil];
    }
    
}



- (void)setAdcrId:(NSString *)adcrId {
    _adcrId = adcrId;
    
    [self loadData];
}

/**
 加载数据
 */
- (void)loadData {
    
    [kNetworkManager requestMyReleasePassengerCooperatioonDetailWithCooperationId:self.adcrId success:^(SISSPassengerCooperationDetailModel *passengerCooperationDetailModel) {
        
        self.passengerCooperationModel = passengerCooperationDetailModel;
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


/**
 删除
 */
- (void)deleteClickAction {
    
    [kNetworkManager deletePassengerCooperationWithPassengerAdcrId:self.adcrId successBlock:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 删除成功
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"删除成功！" andTime:1.0 andColor:YES];
            }];
            if (self.deleteComplete) {
                self.deleteComplete();
            }
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SISSMyReleaseCooperationViewController class]]) {
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

    
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"删除" message:@"删除后不可恢复,您确定删除吗？" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
//    
//    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        
//        NSLog(@"删除");
//            }];
//    
//    [alertController addAction:cancelAction];
//    [alertController addAction:confirmAction];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
}


@end
